package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysOrder;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.service.ISysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 订单信息管理Controller
 *
 * @author Kelsey
 */
@RestController
@RequestMapping("/system/order")
public class SysOrderController extends BaseController {

    @Autowired
    private ISysOrderService orderService;

    @Autowired
    private SysOrderMapper orderMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询订单列表（分页）
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOrder order) {
        IPage<SysOrder> list = orderMapper.selectOrderList(getPage(), order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOrder order) {
        List<SysOrder> list = orderService.selectOrderList(order);
        ExcelUtil<SysOrder> util = new ExcelUtil<>(SysOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 根据订单编号查询订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo) {
        return success(orderService.selectOrderByNo(orderNo));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysOrder order) {
        // 校验订单是否存在
        if (!orderService.checkOrderExist(order)) {
            return AjaxResult.error("新增订单编号" + order.getOrderNo() + "失败，订单已存在");
        }
        // 初始化订单信息
        order.setOrderNo(IdUtils.simpleUUID());
        order.setDeptId(getDeptId());
        order.setUserId(getUserId());
        order.setDeptName(deptMapper.selectDeptById(getDeptId()).getDeptName());
        order.setUserName(getUsername());
        order.setOrderStatus("待付款");
        order.setCreateBy(getUsername());
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysOrder order) {
        order.setUpdateBy(getUsername());
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(orderService.deleteOrderByIds(orderIds));
    }
}